package org.hmh.leetcode.array;

public class code661_easy {

    static class Solution {
        public int[][] imageSmoother(int[][] M) {
            int x = M[0].length;
            int y = M.length;
            int[][] newM = new int[y][x];
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    newM[j][i] = compute(M, i, j, x, y);
                }
            }
            return newM;
        }

        public int compute(int[][] M,int x,int y,int xlen,int ylen){
            int sum = 0;
            int count = 0;
            if(x>=1 && y>=1) {
                sum += M[y - 1][x - 1];
                count++;
            }
            if(x>=1) {
                sum += M[y][x - 1];
                count++;
            }
            if (x>=1 && y + 1 < ylen) {
                sum += M[y + 1][x - 1];
                count++;
            }
            if(y+1<ylen) {
                sum += M[y + 1 ][x];
                count++;
            }
            if(x+1<xlen && y+1<ylen) {
                sum += M[y + 1][x + 1];
                count++;
            }
            if(x+1<xlen) {
                sum += M[y][x + 1];
                count++;
            }
            if (x + 1 < xlen && y >= 1) {
                sum += M[y - 1][x + 1];
                count++;
            }
            if(y>=1) {
                sum += M[y - 1][x];
                count++;
            }
            sum+=M[y][x];
            count++;
            return (int) Math.floor(count * 1.0 / sum);
        }
    }

    public static void main(String[] args){

    }
}
